package services

import (
	"gin-study/app/dao"
	"gin-study/app/http/requests"
	"gin-study/app/models"
)

var Order *order

func init() {
	Order = new(order)
}

type order struct {
}

func (this *order) GetOrderById(id interface{}) (order *models.OrderDetail, err error) {
	err = dao.Order.Preload("User").Take(&order, id).Error
	return
}

func (this *order) GetOrderList(orderSn string, status string, userId uint, page *requests.Page) *models.Paginator {
	tx := dao.Order.Scopes(
		dao.Order.ScopeStatus(status),
		dao.Order.ScopeOrderSn(orderSn),
		dao.Order.ScopeUserId(userId),
	).Preload("User").Order("id DESC")
	var orders []models.OrderList
	return dao.Paginate(tx, &orders, page.Page, page.Limit)
}

func (this *order) CreateOrder(user *models.User) (order *models.Order, err error) {

	order.UserId = user.ID
	order.OrderSn = this.GetOrderSn()

	dao.Order.Save(&order)

	return
}

func (this *order) GetOrderSn() string {
	return "order_sn"
}
